package com.wnhz.work.jy.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.view.PhotoView;

import java.util.ArrayList;
import java.util.List;




public class ShowImageDetail extends BaseActivity {

    private ViewPager mViewPager;
    private ImageDetailAdapter mPagerAdapter;
    private List<String> mImageInfos;
    private int index;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_image_detail);

        index = getIntent().getIntExtra("index", -1);

        mViewPager = (ViewPager) findViewById(R.id.view_pager);

        mImageInfos = getIntent().getStringArrayListExtra("paths");

        System.out.println("mImageInfos:==" + mImageInfos.size());

        mPagerAdapter = new ImageDetailAdapter(this, mImageInfos);
        mViewPager.setAdapter(mPagerAdapter);
        if (index != -1) {
            mViewPager.setCurrentItem(index);
        }
        mViewPager.addOnPageChangeListener(onPageChangeListener);
    }


    private class ImageDetailAdapter extends PagerAdapter {

        private List<String> mData = new ArrayList<>();
        private Context mContext;


        public ImageDetailAdapter(Context context, List<String> data) {
            this.mContext = context;
            this.mData = data;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            PhotoView view = new PhotoView(ShowImageDetail.this);
            view.enable();
            view.setScaleType(ImageView.ScaleType.FIT_CENTER);

            if (mData.get(position).contains("http")) {
                ImageLoader.getInstance().displayImage(mData.get(position), view);
            } else {
                ImageLoader.getInstance().displayImage("file://" + mData.get(position), view);
            }

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShowImageDetail.this.finish();
                }
            });
            container.addView(view);
            return view;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        public String getItem(int position) {
            return mData.get(position);
        }
    }


    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {


        }
    };
}
